package org.example.controller;

import org.example.config.MenuTree;
import org.example.domain.Menu;
import org.example.result.Result;
import org.example.service.MenuService;
import org.example.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @RequestMapping("menuList")
    public List<MenuVo> list(@RequestParam("id") Integer userId){
        List<MenuVo> list = menuService.list(userId);
        List<MenuVo> menuVos = MenuTree.toTree(list);
        return menuVos;
    }

    @RequestMapping("listAll")
    public Result listAll(Menu menu){
        List<MenuVo> list =  menuService.listAll(menu);
        List<MenuVo> menuVos = MenuTree.toTree(list);
        return Result.success(menuVos);
    }

    @RequestMapping("save")
    public Result save(Menu menu){
        menuService.save(menu);
        return Result.success();
    }

    @RequestMapping("remove")
    public Result remove(Integer id){
        menuService.remove(id);
        return Result.success();
    }

    @RequestMapping("/update")
    public Result update(Menu menu){
        menuService.updateMenu(menu);
        return Result.success();
    }

}
